/**
 * \file
 */
#include "precompiled.hpp"
#include "kfs.hpp"
#include "kfs_cache.hpp"
#include "kfs_file_descriptor.hpp"
#include "kfs_impl.hpp"
#include <cstdio>
#include <iostream>
#include <string>
#include <fcntl.h>            // O_CREAT et al

using std::cerr;  using std::cin;  using std::cout;  using std::endl;
using std::ios;
using std::string;


void kfs::test_resolveDiskBlock()
{
    boost::scoped_ptr<FileDescriptor> file(file_open("/", O_RDONLY));
    Inode const& inode = file->inodeRef->elt->second->inode;
    // Number of data blocks in this file.
    uint32_t const numBlocks = impl->fileNumBlocks(inode);
    cout << "File's data is located at disk blocks: ";
    for(uint32_t i = 0; i < numBlocks; ++i)
    {
        uint32_t dataDiskBlockID = impl->file_resolveDiskBlock(
            *file->inodeRef, i);
        cout << dataDiskBlockID << " ";
    }
    cout << endl;
}


void kfs::test_getBlockNumber(Inode const* inode)
{
    // Block numbers are relative to the file.
    //for(uint32_t fileBlockNumber = 0; fileBlockNumber != 4110;
    //    ++fileBlockNumber)
    //{
    //    std::vector<uint32_t> indices;
    //    file_getIndices(fileBlockNumber, indices);
    //    {
    //        uint32_t loworder = fileBlockNumber;
    //        if(loworder >= 12)
    //            loworder -= 12;
    //        loworder &= 0x3FF;
    //        if(loworder >= 2 && loworder <= 1021)
    //            continue;
    //    }

    //    printf("Block %5d: ", fileBlockNumber);
    //    for(std::vector<uint32_t>::const_iterator i = indices.begin();
    //        i != indices.end(); ++i)
    //    {
    //        if( i != indices.begin())
    //            printf(", ");
    //        printf("%5d", *i);
    //    }
    //    printf("\n");
    //}

    uint32_t const n = 1024;
    uint32_t bns[] = {
        0, 1, 2,
        12, 13, 14, n+12-2, n+12-1, n+12, n+12+1,
        //n*n - n + 12 - 2, n*n - n + 12 - 1, n*n - n + 12, n*n - n + 12 + 1,
        n*n + 12 - 2, n*n + 12 - 1, n*n + 12, n*n + 12 + 1,
        n*n + n + 12 - 2, n*n + n + 12 - 1, n*n + n + 12, n*n + n + 12 + 1,
    };
    for(size_t i = 0; i < sizeof(bns)/sizeof(uint32_t); ++i)
    {
        std::vector<uint32_t> indices;
        impl->file_getIndices(bns[i], indices);
        printf("Block %5d: ", bns[i]);
        for(std::vector<uint32_t>::const_iterator i = indices.begin();
            i != indices.end(); ++i)
        {
            if( i != indices.begin())
                printf(", ");
            printf("%4d", *i);
        }
        printf("\n");
    }
}
